package com.tmt.sm.impl;

import com.tmt.sm.ISmSender;
import lombok.Getter;
import lombok.Setter;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLEncoder;
import java.text.MessageFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 凌凯科技公司的短信发送器类
 */
@Getter
public class LkSmSender implements ISmSender{

    private String senderName = "凌凯科技";

    private String corpId;

    private String password;

    private String sendUrl;

    public LkSmSender(String corpId, String password, String sendUrl){
        this.corpId = corpId;
        this.password = password;
        this.sendUrl = sendUrl;
    }

    /**
     * 发送短信。
     * @param toMobile 接收短信的手机号码
     * @param content 发送内容
     * @param sendTime 设定的发送时间, 格式“yyyy-MM-dd HH:mm:ss”
     * @param returnMode 要求的返回模式
     * @return
     */
    @Override
    public SendResult send(String toMobile, String content, Date sendTime, ReturnMode returnMode) {
        int code = 0;
        try{
            String send_time = sendTime == null ? "" : new SimpleDateFormat("yyyyMMddHHmmss").format(sendTime);
            String send_content = URLEncoder.encode(content.replaceAll("<br/>", " "), "GBK");
            String send_url = MessageFormat.format(sendUrl, corpId, password, toMobile, send_content, send_time);
            URL url = new URL(send_url);
            BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
            code = new Integer(in.readLine());
        } catch (Exception e) {
            e.printStackTrace();
            code = -104;
        }
        return getResult(code);
    }


    /**
    大于0的整数	提交成功
    –1	账号未注册
    –2	其他错误
    –3	帐号或密码错误
    –5	余额不足，请充值
    -7	提交信息末尾未加签名，请添加中文的企业签名【 】或内容乱码
    –6	定时发送时间不是有效的时间格式
    –8	发送内容需在1到300字之间
    -9	发送号码为空
    -10	定时时间不能小于系统当前时间
    -100	IP黑名单
    -102	账号黑名单
    -103	IP未导白
     */
    private SendResult getResult(int resultCode) {
        String msg = "发送成功";
        switch (resultCode){
            case -1:
                msg = "账号未注册";
                break;
            case -2:
                msg = "其他错误";
                break;
            case -3:
                msg = "帐号或密码错误";
                break;
            case -5:
                msg = "余额不足";
                break;
            case -7:
                msg = "提交信息末尾未加签名";
                break;
            case -8:
                msg = "发送内容需在1到300字之间";
                break;
            case -9:
                msg = "发送号码为空";
                break;
            case -10:
                msg = "定时时间不能小于系统当前时间";
                break;
            case -100:
                msg = "IP黑名单";
                break;
            case -102:
                msg = "账号黑名单";
                break;
            case -103:
                msg = "IP未导白";
                break;
            case -104:
                msg = "本地调用异常";
                break;
        }
        return new SendResult(getSenderName(), resultCode, msg, "" +resultCode, resultCode >= 0);
    }

    @Override
    public boolean support(ReturnMode returnMode) {
        return ReturnMode.SYNC_MODE == returnMode;
    }
}
